<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/shop/include :: header"></head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group" id="form">
                <div class="layui-form-item">
                    <label class="layui-form-label">请输入角色名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="roleName" id="roleName" lay-verify="required|roleName"
                               autocomplete="off"
                               placeholder="请输入角色名称"
                               class="layui-input" required="required">
                    </div>
                </div>
                <input type="hidden" name="menuIds" id="menuIds">
                <div class="layui-form-item">
                    <label class="layui-form-label">请选择菜单</label>
                    <div class="layui-input-block">
                        <div id="demo1" class="xm-select-demo" style="width: 891px"></div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div th:include="/shop/include::footer"></div>
<script src="../../../layuiadmin/modules/xmSelect.js"></script>
<script>
    layui.use(['layer', 'jquery', 'form', 'laydate', "upload"], function () {
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var upload = layui.upload;
        var form = layui.form;

        ['showFolderIcon', 'showLine', 'strict'].forEach(function (key) {
            layui.form.on('checkbox(' + key + ')', function (data) {
                var treeConfig = {};
                treeConfig[key] = data.elem.checked;
                demo1.update({
                    tree: treeConfig
                })
            });
        })

        var demo1 = xmSelect.render({
            el: '#demo1',
            autoRow: true,
            filterable: true,
            tree: {
                show: true,
                showFolderIcon: true,
                showLine: true,
                indent: 20,
                expandedKeys: [-3],
                strict: false
            },
            toolbar: {
                show: true,
                list: ['ALL', 'REVERSE', 'CLEAR']
            },
            autoRow: true,
            filterable: true,
            height: 'auto',
            data: []
        })

        $.ajax({
            url: '/platform/menu/selectLevelIsOneAndTwo',
            type: "get",
            success: function (result) {
                var res = result.data;
                var str = "[";
                for (var i = 0; i < res.length; i++) {
                    str += "{name: '" + res[i].menuName + "', value: " + res[i].id + ", children:[";
                    for (var l = 0; l < res[i].list.length; l++) {
                        str += "{name: '" + res[i].list[l].menuName + "', value: " + res[i].list[l].id + "},";
                    }
                    str += "]},"
                }
                str += "]";
                demo1.update({
                    data: eval("(" + str + ")")
                })
            },
            error: function (result) {
                layer.msg("查询菜单失败")
            }
        });

        /* 监听提交 */
        form.on('submit(component-form-demo1)', function (data) {
            var selectArr = demo1.getValue('valueStr');
            selectArr = JSON.stringify(selectArr, null, 2);
            selectArr = selectArr.replace(/"/g, "");
            $("#menuIds").val(selectArr);
            data.field.menuIds = selectArr
            $.ajax({
                url: '/platform/platformrole/add',
                type: "post",
                data: JSON.stringify(data.field),
                contentType: 'application/json; charset=utf-8', // 很重要
                async: false,
                success: function (result) {
                    layer.msg(result.message, {
                        offset: '15px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        window.parent.location.reload();
                    });
                }
            });
            return false;
        });
    });
</script>
</body>
</html>

